/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mindbox.mueblesalpes.negocio.persistence;

import com.mindbox.mueblesalpes.negocio.exception.TiendaException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author jsanchez
 */
@Stateless
public class PersistenciaBean implements PersistenciaLocal {
    
    @PersistenceContext
    private EntityManager em;

    public PersistenciaBean() {
    }

    public void create(Object obj) {
        em.persist(obj);
    }

    public void update(Object obj) {
        em.merge(obj);
    }

    public void delete(Object obj) {
        em.remove(em.merge(obj));
    }

    public List findAll(Class c) {
       return em.createQuery("select O from " + c.getSimpleName() + " as O").getResultList();
    }

    public Object findById(Class c, Object id) {
        return em.find(c, id);
    }

    public List findByFilter(String entityName, Map<String, String> parameters) throws Exception{
		try{
            boolean primerParametro = true;
			StringBuffer strQuery = new StringBuffer("SELECT p FROM "+entityName+" p where");
			for (Iterator<String> iterParams = parameters.keySet().iterator(); iterParams.hasNext();) {
                String param = iterParams.next();
                if(primerParametro){
                    if(parameters.get(param) != null){
                        strQuery.append(" p."+param+" like '%"+parameters.get(param)+"%'");
                        primerParametro = false;
                    }
                }else{
                    if(parameters.get(param) != null){
                        strQuery.append(" and p."+param+" like '%"+parameters.get(param)+"%'");
                    }
                }
			}
			return em.createQuery(strQuery.toString()).getResultList();
		}catch(Exception e){
			throw e;
		}
	}

    @Override
    public List findByFilterEnum(String entityName, Map<String, String> parameters, Map<String,Enum> paramsEnum){
		try{
            boolean primerParametro = true;
			StringBuffer strQuery = new StringBuffer("SELECT p FROM "+entityName+" p ");
            if(parameters != null){
                for (Iterator<String> iterParams = parameters.keySet().iterator(); iterParams.hasNext();) {
                    String param = iterParams.next();
                    if(primerParametro){
                        if(parameters.get(param) != null){
                            strQuery.append(" where p."+param+" like '%"+parameters.get(param)+"%'");
                            primerParametro = false;
                        }
                    }else{
                        if(parameters.get(param) != null){
                            strQuery.append(" and p."+param+" like '%"+parameters.get(param)+"%'");
                        }
                    }
                }
            }

            if(paramsEnum != null){
                for (Iterator<String> iterParams = paramsEnum.keySet().iterator(); iterParams.hasNext();) {
                    String enumKey = iterParams.next();
                    if(primerParametro){
                        if(paramsEnum.get(enumKey) != null){
                            strQuery.append(" where p."+enumKey+" = "+ paramsEnum.get(enumKey).getClass().getName() +"."+paramsEnum.get(enumKey));
                            primerParametro = false;
                        }
                    }else{
                        if(paramsEnum.get(enumKey) != null){
                            strQuery.append(" and p."+enumKey+" = "+ paramsEnum.get(enumKey).getClass().getName() +"."+paramsEnum.get(enumKey));
                        }
                    }
                }
            }
			return em.createQuery(strQuery.toString()).getResultList();
		}catch(Exception e){
			throw new TiendaException(e.getMessage()) ;
		}
	}
 
}
